package seqlist.oj;

/**
 * 输出倒数第K个节点
 */
public class NewCoder_KNodes {
    public ListNode FindKthToTail(ListNode head,int k) {
        // 注意判断边界条件
        if (head == null || k == 0) {
            return null;
        }
        ListNode low = head,fast = head;
        // 让fast先走k步
        for (int i = 0; i < k; i++) {
            if (fast == null) {
                // 此时k > list的长度
                return null;
            }
            fast = fast.next;
        }
        while (fast != null) {
            low = low.next;
            fast = fast.next;
        }
        return low;
    }
}
